/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medico;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ren
 */
@WebServlet(name = "richiamo", urlPatterns = {"/richiamo"})
public class richiamo extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {


            ServletContext sc = getServletContext();
            Connection con = (Connection) sc.getAttribute("con");

            /*
             * Recupera il parametro tempo, che serve a generare la lista dei pazienti vaccinati
             * prima di quel periodo.
             */
            String tempo = request.getParameter("tempo");

            String comand = "";
            int molt = 1;
            
            /*
             * se il tempo non è in secondi applica una opportuna costante moltiplicativa.
             * 
             */
            if (tempo.equalsIgnoreCase("anni")) {

                molt = 31557600;
            } else if (tempo.equalsIgnoreCase("giorni")) {
                molt = 86400;
            } else if (tempo.equalsIgnoreCase("mesi")) {
                molt = 2629800;
            }

            int valore = Integer.valueOf(request.getParameter("richiamo_da")).intValue();
            int valoreQuery = valore * molt;
            
            /*
             * recupera i dati dal database
             */
            comand = "SELECT * FROM ultimo_vaccino where TIME_TO_SEC(TIMEDIFF(now(),data_ultimo_vaccino)) >= " + valoreQuery + ";";

            Statement st = null;
            ResultSet rs = null;
            try {
                st = (Statement) con.createStatement();
                rs = (ResultSet) st.executeQuery(comand);

                /*
                 * genera la pagina html
                 */
                out.println("<!DOCTYPE html>  ");
                out.println("<html> ");
                out.println("    <head> ");
                out.println("        <title>Lista richiamo</title> ");
                out.println("    </head> ");
                out.println("    <body> ");
                out.println("        <form action=\"ConfermaGenPDF\" method=\"post\">  ");
                out.println("            <table border=1> ");
                out.println("                <tr bgcolor=\"#DEDFF7\"> ");
                out.println("                    <td> Nome ");
                out.println("                    </td> ");
                out.println("                    <td> Cognome ");
                out.println("                    </td> ");
                out.println("                    <td> Foto ");
                out.println("                    </td> ");
                out.println("                    <td> Data ultimo richiamo ");
                out.println("                    </td> ");
                out.println("                    <td> Ora ultimo richiamo ");
                out.println("                    </td> ");
                out.println("                    <td> ");
                out.println("                    Sel. ");
                out.println("                    </td> ");
                out.println("                    <td> ");
                out.println("                    Info ");
                out.println("                    </td> ");
                out.println("                    <td> ");
                out.println("                    Lista");
                out.println("                    </td> ");
                out.println("                </tr>");
                out.println("                <tr bgcolor=\"#DEDFF7\"> ");
                out.println("                <td>");
                out.println("            <input type=\"submit\" value=\"Generare PDF\" />  ");
                out.println("        </form>");
                out.println("                </td>");
                out.println("                <form action=\"logoff\" method=\"post\"> ");
                out.println("                    <td> ");
                out.println("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
                out.println("                    </td>                   ");
                out.println("                </form> ");
                out.println("                <form action=\"welcome\" method=\"post\"> ");
                out.println("                    <td> ");
                out.println("                        <input type=\"submit\"  value=\"Tornare\"/>                         ");
                out.println("                    </td>                   ");
                out.println("                </form> ");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                </tr>");


                boolean c = false;
                while (rs.next()) {

                    if (c) {
                        out.println("<tr bgcolor=\"#E6E0E0\">");
                        c = false;
                    } else {
                        out.println("<tr bgcolor=\"white\">");

                        c = true;
                    }
                    String realPath = getServletContext().getContextPath();

                    out.println("                    <td>");
                    out.println(rs.getString(1));
                    out.println("                    </td> ");
                    out.println("                    <td>");
                    out.println(rs.getString(2));
                    out.println("                    </td> ");
                    out.println("                    <td>");
                    out.println("<IMG SRC=\"" + realPath + rs.getString(3) + "\"  WIDTH=\"60\" HEIGHT=\"60\" ALT=\" " + rs.getString(3) + "\" \\>");
                    out.println("                    </td> ");
                    out.println("                    <td>");

                    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

                    out.println(dateFormat.format(rs.getDate(4)));
                    out.println("                    </td> ");
                    out.println("                    <td>");

                    dateFormat = new SimpleDateFormat("HH:mm:ss");

                    out.println(dateFormat.format(rs.getTime(4)));
                    out.println("                    </td> ");
                    out.println("                    <td> ");
                    out.println("                         <input type=\"checkbox\" name=\"idpaz\" value=\"" + rs.getString(5) + "\" /> ");
                    out.println("                    </td> ");
                    out.println("<td>");
                    out.println("<a href=\"profiloUtente?id=" + rs.getString(5) + "\"><IMG SRC=\"images/lupa.gif\"  WIDTH=\"20\" HEIGHT=\"20\" ALT=\"Detagli\"> </a>");
                    out.println("</td>");
                    out.println("<td>");
                    out.println("<a href=\"listaVaccinazione?idpaziente=" + rs.getString(5) + "\"><IMG SRC=\"images/lista.gif\"  WIDTH=\"20\" HEIGHT=\"20\" ALT=\"Detagli\"> </a>");
                    out.println("</td>");
                }


                out.println("                <tr bgcolor=\"#DEDFF7\"> ");
                out.println("                <td>");
                out.println("            <input type=\"submit\" value=\"Generare PDF\" />  ");
                out.println("        </form>");
                out.println("                </td>");
                out.println("                <form action=\"logoff\" method=\"post\"> ");
                out.println("                    <td> ");
                out.println("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
                out.println("                    </td>                   ");
                out.println("                </form> ");
                out.println("                <form action=\"welcome\" method=\"post\"> ");
                out.println("                    <td> ");
                out.println("                        <input type=\"submit\"  value=\"Tornare\"/>                         ");
                out.println("                    </td>                   ");
                out.println("                </form> ");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                <td>");
                out.println("                </td>");
                out.println("                </tr>");
                out.println("            </table> ");
                out.println("</body> ");
                out.println("</html> ");

            } catch (SQLException ex) {
                Logger.getLogger(richiamo.class.getName()).log(Level.SEVERE, null, ex);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
